﻿using BuildCode.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BuildCode.Models
{
    public class PublicHelper
    {
        public static string GetPropertyName(string columnName)
        {
            var dic = new Dictionary<string, string> {
                { "CREATEUSER", "CreateUser" },
                { "CREATEDATE", "CreateDate" },
                { "UPDATEUSER", "UpdateUser" },
                { "UPDATEDATE", "UpdateDate" },
                { "ORGANIZATIONID", "OrganizationID" },
                { "TENANTID", "TenantID" } };
            if (dic.ContainsKey(columnName)) return dic[columnName];
            string result = "";
            string firstLetter = columnName.Substring(0, 1);
            string last = columnName.Substring(1, columnName.Length - 1);
            result += firstLetter + last.ToLower();
            return result;
        }
        public static string SplitComment(string v)
        {
            if (!string.IsNullOrEmpty(v))
            {
                var index = v.IndexOf("(");
                index = index <= 0 ? v.IndexOf("（") : index;
                if (index >= 0)
                {
                    v = v.Substring(0, index);
                }
                v = v.Replace("ID", "");
            }
            return v;
        }

        public static string MapCsharpType(string dbtype, bool NotNull)
        {
            string strNull = NotNull ? "" : "?";
            if (string.IsNullOrEmpty(dbtype)) return dbtype;
            if (dbtype.Contains("CHAR"))
                return "string";
            if (dbtype.Contains("DECIMAL"))
                return "decimal" + strNull;
            string csharpType = "object";
            var dic = new Dictionary<string, string> {
                {"NUMBER","int"+strNull },
                {"DATE","DateTime" + strNull },
                {"INTEGER","int" + strNull},
                {"SMALLINT","short" + strNull}
            };
            if (dic.ContainsKey(dbtype))
                return dic[dbtype];
            return csharpType;
        }

        public static string MapCsharpType2(string dbtype, bool NotNull)
        {
            string strNull = NotNull ? "" : "?";
            if (string.IsNullOrEmpty(dbtype)) return dbtype;
            dbtype = dbtype.ToLower();
            string csharpType = "object";
            switch (dbtype)
            {
                case "bigint": csharpType = "long"; break;
                case "binary": csharpType = "byte[]"; break;
                case "bit": csharpType = "bool" + strNull; break;
                case "char": csharpType = "string"; break;
                case "date": csharpType = "DateTime" + strNull; break;
                case "datetime": csharpType = "DateTime" + strNull; break;
                case "DateTime": csharpType = "DateTime" + strNull; break;
                case "datetime2": csharpType = "DateTime" + strNull; break;
                case "datetimeoffset": csharpType = "DateTimeOffset"; break;
                case "decimal": csharpType = "decimal" + strNull; break;
                case "float": csharpType = "double" + strNull; break;
                case "image": csharpType = "byte[]"; break;
                case "int": csharpType = "int" + strNull; break;
                case "money": csharpType = "decimal" + strNull; break;
                case "nchar": csharpType = "string"; break;
                case "ntext": csharpType = "string"; break;
                case "numeric": csharpType = "decimal" + strNull; break;
                case "nvarchar": csharpType = "string"; break;
                case "real": csharpType = "Single"; break;
                case "smalldatetime": csharpType = "DateTime" + strNull; break;
                case "smallint": csharpType = "short"; break;
                case "smallmoney": csharpType = "decimal" + strNull; break;
                case "sql_variant": csharpType = "object"; break;
                case "sysname": csharpType = "object"; break;
                case "text": csharpType = "string"; break;
                case "time": csharpType = "TimeSpan"; break;
                case "timestamp": csharpType = "byte[]"; break;
                case "tinyint": csharpType = "byte"; break;
                case "uniqueidentifier": csharpType = "Guid" + strNull; break;
                case "varbinary": csharpType = "byte[]"; break;
                case "varchar": csharpType = "string"; break;
                case "xml": csharpType = "string"; break;
                default: csharpType = "object"; break;
            }
            return csharpType;
        }

        public static string CreateTable(List<ColumnInfo> List, ConfigInfo Config)
        {
            Config.TableName = Config.TableName.Trim().ToUpper();
            List<string> strCreateTable = new List<string>();
            List<string> strCreateComment = new List<string>();
            List = List.Where(t => t.IsDataColumn == true).ToList(); //过滤非数据库字段
            strCreateTable.Add($"CREATE TABLE \"LEILI_ABP_MES\".\"{Config.TableName}\"(");
            //strCreateComment.Add($"EXEC sp_addextendedproperty N'MS_Description', N'{Config.TableComment}', 'SCHEMA', N'dbo', 'TABLE', N'{Config.TableName}', NULL, NULL; ");
            int index = 0;
            foreach (var item in List)
            {
                //string length = item.TypeName.ToLower().Contains("varchar") ? $"({item.MaxLength.ToString()})" : "";
                //if (item.TypeName == "VARCHAR2")
                //{
                //    length = $"({item.MaxLength.ToString()} BYTE)";
                //}
                strCreateTable.Add(string.Format($"\"{item.ColumnName.Trim()}\" {item.TypeName}{{0}} {{1}} {{2}}{{3}}",
                    "",
                    item.NotNUll ? "NOT NULL" : "NULL",
                    item.DefaultValue?.Length > 0 ? $" DEFAULT '{item.DefaultValue}'" : "",
                    index == List.Count - 1 ? "":","
                    ));
                strCreateComment.Add($"COMMENT ON COLUMN \"LEILI_ABP_MES\".\"{Config.TableName}\".\"{item.ColumnName.Trim()}\" IS '{item.Comment}';");
                index++;
            }
            strCreateComment.Add($"comment on table \"{Config.TableName}\" is '{Config.TableComment}'; ");
            strCreateTable.Add($");\r\n");
            return string.Join("\r\n", strCreateTable) + string.Join("\r\n", strCreateComment);
        }

    }
}